#include "../headers/main.h"

void system_shutdown();

void about();

//日常任务：输出一条欢迎提示、载入链表、登陆并导向不同GUI
int main() {
    init(); //初始化链表
    struct time_item now;
    get_timeItem(&now);
    system_clear();
    print_welcome();

    if (access("./data/", F_OK) == -1)  //判断data文件夹是否存在，不存在则创建
        folder("./data/");
    if (!isFileOK("./data/shopInfo.dat")) { //检查是否是第一次运行
        printf("您似乎是第一次使用本系统，首先请您进行初始化设定\n");
        printf("您的店铺名称：");
        while (!input_string(shop.name, 64));   //加上while强制输入（下同）
        printf("您的启动资金：");
        while (!input_double(&shop.startMoney));
        get_timeItem(&shop.buildTime);
        shop.autoBackup = 0;
        write_shopInfo("./data/"); //写入文件

        puts("✅ 初始化完成，请创建管理员账户（此账户必须建立，有且仅有一个）");
        while (!user_register('G'));
        system_clear();
        puts("✅ 创建成功，请登陆");
    } else {
        int dayPassed = get_dayPassed(shop.buildTime, now);
        printf("尊敬的%s店员，欢迎您！今天是%d年%d月%d日，是您使用本系统的第%d天。\n", shop.name, now.year, now.month, now.day, dayPassed + 1);
    }

    // 开始主循环
    while (1) {
        printf("⏳正在登陆到%s的后台管理系统...\n", shop.name);
        current_user = user_login();
        if (current_user == NULL) continue;
        setCheck(); //设置后台任务（过期提醒）

        if (current_user->type == 'L') {    //理货员功能板块
            for (bool logout = false; !logout;) {
                puts("请选择您要使用的系统功能：");
                print_menu("[1]进货系统\t[2]库存管理系统\t[3]上架系统\t[4]退出登陆\t[5]关闭系统");
                int choice;
                while (!input_int(&choice, 1, 5));
                switch (choice) {
                    case 1:
                        restock();
                        break;
                    case 2:
                        storage();
                        break;
                    case 3:
                        shelf();
                        break;
                    case 4:
                        logout = true;
                        break;
                    case 5:
                        system_shutdown();
                        exit(0);
                }
                system_clear(); //清屏
            }
        }

        if (current_user->type == 'X') {    //销售员功能板块
            for (bool logout = false; !logout;) {
                puts("请选择您要使用的系统功能：");
                print_menu("[1]零售收银系统\t[2]批发系统\t[3]会员管理系统\t[4]退出登陆\t[5]关闭系统");
                int choice;
                while (!input_int(&choice, 1, 5));
                switch (choice) {
                    case 1:
                        check_out();
                        break;
                    case 2:
                        wholesale_system();
                        break;
                    case 3:
                        VIP_manager();
                        break;
                    case 4:
                        logout = true;
                        break;
                    case 5:
                        system_shutdown();
                        exit(0);
                }
                system_clear(); //清屏
            }
        }

        if (current_user->type == 'G') {    //管理员功能板块
            for (bool logout = false; !logout;) {
                puts("请选择您要使用的系统功能：");
                print_menu("[1]信息管理系统\t[2]营收统计系统\t[3]用户管理系统\t[4]备份系统\t[5]关于\t[6]退出登陆\t[7]关闭系统");
                int choice;
                while (!input_int(&choice, 1, 7));
                switch (choice) {
                    case 1:
                        infoManager();
                        break;
                    case 2:
                        money_manager_system(head_sellMessage, head_purchaseMessage);
                        break;
                    case 3:
                        userManager(current_user);
                        break;
                    case 4:
                        backup_system();
                        break;
                    case 5:
                        about();
                        break;
                    case 6:
                        logout = true;
                        break;
                    case 7:
                        system_shutdown();
                        exit(0);
                }
                system_clear(); //清屏
            }
        }
    }
}

//关于页面
void about() {
    system_clear();
    print_iShop();
    puts("\nContributor: AQiu LordRosan YuLuoYang LinABei\n");
    puts("----- iShop果蔬商店管理系统™️️2022-2022 -----");
    system_pause();
}

//关闭系统：写入备份、文件，释放链表
void system_shutdown() {
    checkPoint("./data/");
    if (shop.autoBackup) {
        char path[64] = "./backup/";
        if (access(path, F_OK) == -1)
            folder(path);
        struct time_item now;
        get_timeItem(&now);
        sprintf(path, "./backup/%d%02d%02d-%02d%02d%02d/",
                now.year, now.month, now.day, now.hour, now.minute, now.second);
        folder(path);
        checkPoint(path);
        puts("✅ 自动备份成功！");
    }
    //释放所有链表
    fruit_free(head_fruitStored);
    fruit_free(head_fruitOnShelf);
    purchaseMessage_free(head_purchaseMessage);
    sellMessage_free(head_sellMessage);
    account_free(head_user);
    account_free(head_VIP);
    puts("🤘 系统正在关闭，数据已安全保存，感谢您的使用！");
    system_pause();
}